<template>
  <transition name="slide-left-in">
    <div v-if="visible" class="record-list shadow">
      <div class="max-w-lg m-auto py-5 min-h-screen relative">
        <ul v-if="records.length">
          <li class="flex justify-between px-10 py-5 text-lg" 
            v-for="n in records" :key="n.timeStamp">
            <span class="font-bold">{{ getDateString(n.timeStamp) }}</span>  
            <span class="badge badge-lg">{{ mathLabel(n.status) }}</span>
          </li>
        </ul>
        <div class="text-center py-10 text-lg text-slate-500 font-bold">暂无记录</div>
        <div class="w-full px-5 absolute bottom-10">
          <button class="btn btn-block" @click="close">关闭</button>
        </div>
      </div>
    </div>
  </transition>
</template>

<script lang="ts">
  export type RecordPanneType = {
    show: () => void
  }
</script>
<script setup lang="ts">
  import { ref } from 'vue'
  import { getTimeRecord, Status, STATUS_REST } from '../../storage'
  import { padZero } from 'iius'

  const visible = ref(false)
  const records = ref(getTimeRecord())

  const show = () => {
    visible.value = true
    records.value = getTimeRecord()
  }

  const close = () => {
    visible.value = false
  }

  const mathLabel = (status: Status) => {
    return status === STATUS_REST ?  '下班' : '上班'
  }

  const getDateString = (timeStamp: number) => {
    const d = new Date(timeStamp)
    const hour = padZero(d.getHours())
    const minute = padZero(d.getMinutes())
    const second = padZero(d.getSeconds())
    return `${hour}:${minute}:${second}`
  }

  defineExpose<RecordPanneType>({
    show
  })
</script>

<style scoped>
  .record-list {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background-color: #fff;
    transform: translate3d(0, 0, 0);
    z-index: 999;
  }

  .slide-left-in-enter-active,
  .slide-left-in-leave-active {
    transition: all .3s ease;
  }

  .slide-left-in-enter-from,
  .slide-left-in-leave-to {
    transform: translate3d(100%, 0, 0);
  }
</style>